package org.example.web.response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 重定向
 */
@WebServlet("/httpServletDemo1")
public class HttpServletDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("httpServletDemo1_doGet?");
        //重定向
        //设置响应状态码
        //resp.setStatus(302);
        //设置响应头location
        //resp.setHeader("location", "/response-demo/httpServletDemo2");

        //简化方式
        //resp.sendRedirect("/response-demo/httpServletDemo2");
        //动态获取虚拟目录 "/response-demo"
        String contextPath = req.getContextPath();
        System.out.println(contextPath);
        resp.sendRedirect(contextPath + "/httpServletDemo2");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
